package com.tidemark.ui.swing;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Properties;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JToolBar;

import com.tidemark.util.SystemParams;

public class MainFrame extends JFrame {
	
	// The preferred size of the demo
    private static final int PREFERRED_WIDTH = 720;
    private static final int PREFERRED_HEIGHT = 640;
	
	JPanel contentPane;
	ResultPane resultPane;
	SystemParams sysParams;
	
	/*------------ Action List Begin-----------*/
	Action refreshAction;
	Action preferenceAction;
	Action cleanAction;
	Action exitAction;
	Action aboutAction;
	/*------------- Action List End------------*/
	
	public MainFrame() {
		super("Tide Mark");
		
		loadSystemParams();
		initUI();
	}
	
	private void initUI() {
		initActions();
		contentPane = createContentPane();		
		setContentPane(contentPane);		
		setJMenuBar(createMenuBar());
		
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
				closeMainFrame();
			}
		}); 
	}
	
	private JPanel createContentPane() {
		JPanel panel = new JPanel(new BorderLayout());
		resultPane = new ResultPane(this);		
		
        JToolBar toolBar = createToolbar();
        
        panel.add(toolBar, BorderLayout.PAGE_START);
        panel.add(resultPane, BorderLayout.CENTER);
        
        panel.setPreferredSize(new Dimension(PREFERRED_WIDTH,PREFERRED_HEIGHT));
		return panel;
	}
	
	private void initActions() {
		refreshAction = new AbstractAction("Refresh") {
			public void actionPerformed(ActionEvent e) {
				refreshResult();
			} 
		};
		
		preferenceAction = new AbstractAction("Preferences") {
			public void actionPerformed(ActionEvent e) {
				setUp();
			} 
		};
		
		cleanAction = new AbstractAction("CleanGUI(for test)") {
			public void actionPerformed(ActionEvent e) {
				cleanGUI();
			} 
		};
		
		exitAction = new AbstractAction("Exit") {
			public void actionPerformed(ActionEvent e) {
				closeMainFrame();
			} 
		};
		
		aboutAction = new AbstractAction("About") {
			public void actionPerformed(ActionEvent e) {
				showAboutDialog();
			} 
		};
	}
	
	private JToolBar createToolbar() {
		JToolBar toolBar = new JToolBar("TideMark toolbar");
			
		toolBar.add(refreshAction);	
		toolBar.add(preferenceAction);		
		toolBar.add(cleanAction);
		
		toolBar.addSeparator();
		toolBar.add(aboutAction);
		toolBar.add(exitAction);
		
		return toolBar;
	}
	
	private JMenuBar createMenuBar() {
		JMenuBar menuBar = new JMenuBar();
		
		JMenu fileMenu = new JMenu("File");
		fileMenu.add(exitAction);
		menuBar.add(fileMenu);
		
		JMenu actionMenu = new JMenu("Operation"); 
		actionMenu.add(refreshAction);
		actionMenu.add(preferenceAction);
		actionMenu.add(cleanAction);		
		menuBar.add(actionMenu);
		
		JMenu helpMenu = new JMenu("Help");
		helpMenu.add(aboutAction);
		menuBar.add(helpMenu);
		
		return menuBar;
	}
	
	private void loadSystemParams() {
		//Read params from file?
		sysParams = new SystemParams();
		activeSystemParams();
	}
	
	private void saveSystemParams() {
		
	}
	
	private void activeSystemParams() {
		//setup proxy
		Properties sys = System.getProperties();
		if(sysParams.isUseProxy()) {
			sys.put("proxySet","true");
		} else {
			sys.put("proxySet","false");
		}
		sys.put("proxyHost", sysParams.getProxyHost());
		sys.put("proxyPort", String.valueOf(sysParams.getPorxyPort()));
		
		System.setProperties(sys);
		
		//TODO:
		//setup other params
	}
	
	void refreshResult() {
		resultPane.refreshResult();
	}
	
	void setUp() {
		ParamsSettingDialog dlg = new ParamsSettingDialog((SystemParams)sysParams.clone());
		int option = dlg.showDialog(this);
		
		if(option == ParamsSettingDialog.SETUP_OK) {
			sysParams = dlg.getSystemParams();
			activeSystemParams();
		}
	}
	
	void cleanGUI() {
		resultPane.cleanGUI();
	}
	
	void closeMainFrame() {
		saveSystemParams();
		System.exit(0);
	}
	
	void showAboutDialog() {
		JDialog dlg = new AboutDialog(this);
		dlg.setVisible(true);
	}
	
	public static void main(String[] args) {
		JFrame frm = new MainFrame();
		frm.pack();
		frm.setVisible(true);
		
	}
}
